<?php
	
	require_once 'Zend/Db.php';
	
	class UserController extends Zend_Controller_Action
	{
		
		public function indexAction()
		{
			$this->view->titulo = "User";
			$this->view->mensaje = "Pagina de usuario";
			//$this->view->render('index');
		}
		
		public function loginAction()
		{	
			$this->titulo = "Login";
			$this->mensaje = "Login";	
			//$this->view->render('login');			
		}
			
		public function authenticateAction()
		{
			/*
			 * Creamos un enlace a la base de datos
			 * Mysql 
			 */
			$params = array('host'=>'localhost',
							'username'=> 'feeduser',
							'password'=> '123',
							'dbname'=> 'feeds');
					
			$DB = Zend_Db::factory('Pdo_Mysql',$params);
			
			$this->view->sql = "SELECT * FROM user WHERE user = " . $_POST['user']. " AND pass = " . $_POST['pass'];
			
			
			
			//$DB->prepare();
			
			//$rs = $DB->select();
			
		}
		
		function logoutAction()
		{
			
		}
		
		function registerAction()
		{
			//$this->view->render('register');
		}
		
		function createAction()
		{
			$params = array('host'=>'localhost',
							'username'=> 'feeduser',
							'password'=> '123',
							'dbname'=> 'feeds');
			
			//PDO PHP DATA OBJECTS
			$DB = Zend_Db::factory('Pdo_Mysql',$params);
			
			/*
			//Primera manera de hacerlo
			if ($DB != null){	
				$this->view->valor = "Conectado";
				
				$sql = "INSERT INTO user (firstName,lastName,email,user,pass)" . 
				       " VALUES ('". $_POST['fName'] . "','" . $_POST['lName'] . "','" . $_POST['email'] . "','" . $_POST['user'] . "',password('" . $_POST['pass'] . "'))";				
				
				$DB->query($sql);
				
			} else {
				$this->view->valor = "No Conectado";							
			}
			*/
			
			//Segunda manera de hacerlo
			if ($DB != null)
			{
				//Encriptar la clave antes de guardarla
								
				$row = array('firstName' => $_POST['fName'],
						 'lastName'=> $_POST['lName'],
						 'email'=> $_POST['email'],
						 'user'=> $_POST['user'],
						 'pass'=> $_POST['pass']);
				
				$table = 'user';
				
				$rowsAfected = $DB->insert($table,$row);
				
				if ($rowsAfected > 1){
					$this->view->mensaje = "Gracias por registrarse";
				}else{
					$this->view->mensaje = "Intentelo nuevamente";
				}
					
			}
			else 
			{
				$this->view->mensaje = "Error al conectar a la BD";	
			}
			
		}
		
		function displayProfileAction()
		{
			
		}
		
		function updateAction()
		{
			
		}
		
		/*
		 * Specially named by Zend to called whenever the URL refers
		 * to an non existent action.
		 * */
		public function __call($action,$arguments)
		{
			echo '"Action" no existente';
		}
		
	}

?>